package com.artifact.mit.mypoint.fragment.app;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.artifact.mit.mypoint.R;
import com.artifact.mit.mypoint.adapter.TransferPointRightTabAdapter;
import com.artifact.mit.mypoint.service.model.PointIssuer;
import com.artifact.mit.mypoint.service.model.api.PointSource;
import com.artifact.mit.mypoint.service.model.api.PointSource.UserPointSource;
import com.artifact.mit.mypoint.service.util.APICall;

public class TPPointOfMypointFragment extends Fragment {
	
	protected ListView transferPoint = null;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.tp_point_of_mypoint, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		transferPoint = (ListView) getView().findViewById(R.id.listView1);
		new PointSourceLoader().execute("");
		
	}
	
	private class PointSourceLoader extends AsyncTask<String, Void, PointSource> {
		
		private ProgressDialog dialog;
		
		public PointSourceLoader() {
			dialog = new ProgressDialog(getActivity());
			dialog.setCancelable(false);
		}
		
		protected void onPreExecute() {
			this.dialog.setMessage("Loading");
			this.dialog.show();
		}
		
		@Override
		protected void onPostExecute(PointSource results) {
			if (dialog.isShowing()) {
				dialog.dismiss();
			}
			TransferPointRightTabAdapter transferPointAdapter = new TransferPointRightTabAdapter(getActivity());
			for(UserPointSource ps : results.getUserPointSource()){
				transferPointAdapter.add(new PointIssuer(R.drawable.noimage, ps.getSource(), null, null, "Source : "+ps.getValue(), "Exchange : N/A"));
			}
			transferPoint.setAdapter(transferPointAdapter);
		}

		@Override
		protected PointSource doInBackground(String... arg0) {
			PointSource points = APICall.pointSource(getActivity());
			return points;
		}

	}
}
